﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DixitOnline.DixitGame
{
	public static class Extensions
	{
		#region Enumerable extensions
		
		/// <summary>
		/// Picks the random element in a collection
		/// </summary>
		/// <typeparam name="T">Collection type</typeparam>
		/// <param name="source">Source</param>
		/// <returns></returns>

		public static T PickRandom<T>(this IEnumerable<T> source)
		{
			return source.PickRandom(1).Single();
		}

		public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, int count)
		{
			return source.Shuffle().Take(count);
		}

		public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
		{
			return source.OrderBy(x => Guid.NewGuid());
		}

		/// <summary>
		/// Implements a ForEach extension method for enumerable types
		/// </summary>
		/// <typeparam name="T">enumerable type</typeparam>
		/// <param name="enumeration">enumeration</param>
		/// <param name="action">method to be performed on each element</param>
		public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
		{
			foreach (T element in enumeration)
			{
				action(element);
			}
		}

		#endregion
	}
}
